package com.funny.learning.english.adapter;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.funny.learning.english.R;
import com.funny.learning.english.model.GameEntity;

public class TuVungAdapter extends PagerAdapter {

	private ArrayList<GameEntity> arrData;
	private LayoutInflater inflater;
	private Context mContext;
	
	private ClickListener clickListener;
	
	private File file;

	public TuVungAdapter(Context context,File file,
			ArrayList<GameEntity> arrData) {
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.arrData = arrData;
		mContext = context;
		this.file = file;
	}

	@Override
	public int getCount() {
		return arrData.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	/*
	 * @Override public Object instantiateItem(View container, int position) {
	 * View v = inflater.inflate(R.layout.slide_home_item, null); assert v !=
	 * null; container.ad return super.instantiateItem(container, position); }
	 */

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		View v = inflater.inflate(R.layout.tu_vung_item, null);
		assert v != null;

		final GameEntity gameEntity = arrData.get(position);

		ImageView ivDes = (ImageView) v.findViewById(R.id.ivDes);
		ImageView ivPre = (ImageView) v.findViewById(R.id.ivPre);
		ImageView ivSound = (ImageView) v.findViewById(R.id.ivSound);
		ImageView ivRight = (ImageView) v.findViewById(R.id.ivRight);
		TextView tvTiengViet = (TextView)v.findViewById(R.id.tvTiengViet);
		TextView tvTiengAnh = (TextView)v.findViewById(R.id.tvTiengAnh);
		TextView tvPhienAm = (TextView)v.findViewById(R.id.tvPhienAm);
		
		if(position == 0){
			ivPre.setVisibility(View.GONE);
			ivRight.setVisibility(View.VISIBLE);
		}else if(position >= getCount()-1){
			ivPre.setVisibility(View.VISIBLE);
			ivRight.setVisibility(View.GONE);
		}else{
			ivPre.setVisibility(View.VISIBLE);
			ivRight.setVisibility(View.VISIBLE);
		}
		
		if(gameEntity != null){
			tvPhienAm.setText(gameEntity.getNphonetics());
			tvTiengAnh.setText(gameEntity.getLname());
			tvTiengViet.setText(gameEntity.getName());
			Bitmap bm1 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity)),mContext.getResources().getDimensionPixelSize(R.dimen.game_img_small), mContext.getResources().getDimensionPixelSize(R.dimen.game_img_small), true);
			ivDes.setImageBitmap(bm1);
			
			if(gameEntity.getLname() == null || gameEntity.getName() == null){
//				clickListener.onNext();
			}
		}
		
		ivPre.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(position > 0){
					clickListener.onPre();
				}
			}
		});
		
		ivSound.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				clickListener.getSound(gameEntity);
			}
		});
		
		ivRight.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(position < arrData.size() - 1){
					clickListener.onNext();
				}
				
			}
		});
		
		container.addView(v, 0);
		return v;
	}
	
	
	
	public ClickListener getClickListener() {
		return clickListener;
	}

	public void setClickListener(ClickListener clickListener) {
		this.clickListener = clickListener;
	}



	public interface ClickListener{
		void onPre();
		void onNext();
		void getSound(GameEntity gameEntity);
	}
	
	public String getPathImg(GameEntity gameEntity) {
		if (gameEntity.getUrl() == null)
			return null;
		return file.getPath() + File.separator + gameEntity.getUrl();
	}

}
